package com.song.controller;


import com.song.util.FileUpload;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@RestController
@RequestMapping("upload")
public class UploadController {

//    上传图片接口
    @RequestMapping("uploadimage")
    public  String  uploadimage(MultipartFile file) throws Exception {
        FileUpload fileUpload = new FileUpload();


            File targetFile = null;
            try {
                InputStream initialStream = file.getInputStream();
                byte[] buffer = new byte[initialStream.available()];
                initialStream.read(buffer);
                targetFile = new File("src/main/resources/", file.getOriginalFilename());
                OutputStream outStream = null;
                outStream = new FileOutputStream(targetFile);
                outStream.write(buffer);
//                使用 腾讯对象存储 存放照片
                fileUpload.upload(targetFile, file.getOriginalFilename());
                initialStream.close();
                outStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {

                targetFile.delete();
            }


        return "https://songtell-1251684550.cos.ap-chengdu.myqcloud.com/traffic/"+file.getOriginalFilename();
    }
}
